package com.gykj.rtm.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.gykj.admin.common.entity.Role;
import com.gykj.admin.common.entity.RoleResources;
import com.gykj.admin.common.resp.RespResultJson;
import com.gykj.rtm.facade.admin.RoleFacade;

/**
 * Created by rohto.li on 2017/11/03.
 */
@Api(value = "岗位操作接口")
@RestController
@RequestMapping("/roles")
public class RoleControllerFacade {

	@Autowired
	RoleFacade roleFacade;
	
	@ApiOperation(value = "检索岗位" ,  notes="检索岗位,支持条件模糊查询")
    @RequestMapping(value = "/getAllRoleList", method = RequestMethod.POST)
    public RespResultJson getAllRoleList(
    		Role role,
            @RequestParam(value="currentPage",required = false, defaultValue = "1") int currentPage,
            @RequestParam(value="pageSize",required = false, defaultValue = "20") int pageSize,
            @RequestParam(value="sortColumn",required = false, defaultValue = "1") int sortColumn,
            @RequestParam(value="sortDesc",required = false, defaultValue = "1") int sortDesc
    		)
    		{
    	RespResultJson result = new RespResultJson();
    	Map<String,Object> map = new HashMap<>();
    	try{
    		PageInfo<Role> pageInfo = roleFacade.getAllRoleList(role, currentPage, pageSize, sortColumn, sortDesc);
    		map.put("rows", pageInfo.getList());
    		map.put("total", pageInfo.getTotal());
    		
    		result.setStatus(true);
    		result.setResult(map);
    	}catch(Exception e) {
    		result.setStatus(false);
    		result.setMessage(e.getMessage());
    	}
        return result;
    }
	
	@ApiOperation(value = "检索岗位" ,  notes="通过岗位ID来检索岗位")
    @RequestMapping(value = "/getRoleById", method = RequestMethod.GET)
    public RespResultJson getRoleById(@RequestParam(value = "uid",required = false) Integer uid) {
    	RespResultJson result = new RespResultJson();
    	Map<String,Object> map = new HashMap<>();
    	try{
    		List<Role> list = roleFacade.getRoleById(uid);
    		map.put("rows", list);
    		map.put("total", 1);
    		
        	result.setStatus(true);
        	result.setResult(map);
    	}catch(Exception e) {
        	result.setStatus(false);
    		result.setMessage(e.getMessage());
    	}
        return result;
    }
	/*@ApiOperation(value = "检索岗位" ,  notes="检索岗位")
    @RequestMapping(value = "/rolesWithSelected", method = RequestMethod.POST)
    public JsonResult rolesWithSelected(
    		Role role,
            @RequestParam(value="currentPage",required = false, defaultValue = "1") int currentPage,
            @RequestParam(value="pageSize",required = false, defaultValue = "20") int pageSize,
            @RequestParam(value="sortColumn",required = false, defaultValue = "1") int sortColumn,
            @RequestParam(value="sortDesc",required = false, defaultValue = "1") int sortDesc
    		)
    		{
    	JsonResult result = new JsonResult();
    	Map<String,Object> map = new HashMap<>();
    	try{
    		PageInfo<Role> pageInfo = roleFacade.rolesWithSelected(role, currentPage, pageSize, sortColumn, sortDesc);
    		map.put("rows", pageInfo.getList());
    		map.put("total", pageInfo.getTotal());
    		
    		result.setStatus(true);
    		result.setResult(map);
    	}catch(Exception e) {
    		result.setStatus(false);
    		result.setMessage(e.getMessage());
    	}
        return result;
    }*/

	@ApiOperation(value = "给角色分配资源",notes = "给角色分配资源")
    @RequestMapping(value = "/saveRoleResources",method = RequestMethod.POST)
    public RespResultJson saveRoleResources(@RequestBody RoleResources roleResources) {
		RespResultJson result = new RespResultJson();
		if (StringUtils.equals("success", roleFacade.saveRoleResources(roleResources))) {
			result.setStatus(true);
		} else {
			result.setStatus(false);
		}
    	return result;

    }
	
	@ApiOperation(value = "添加角色或者岗位", notes ="添加角色或者岗位")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RespResultJson addRole(@RequestBody Role role) {
		RespResultJson result = new RespResultJson();
		if (StringUtils.equals("success", roleFacade.add(role))) {
			result.setStatus(true);
		} else {
			result.setStatus(false);
		}
    	return result;
    }
	
	@ApiOperation(value = "岗位删除", notes = "更具岗位ID,删除岗位")
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public RespResultJson delRole(@RequestParam(value="id",required=false) Integer id) {
		RespResultJson result = new RespResultJson();
		if (StringUtils.equals("success", roleFacade.delete(id))) {
			result.setStatus(true);
		} else {
			result.setStatus(false);
		}
    	return result;
    }
}
